package com.pruebas.physics.common;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Circle {
	private float radius;
	private int definition;
	
	private float initialX;
	private float initialY;
	
	private FloatBuffer floatBuffer;
	
	public Circle(float radius, int definition){
		this(radius, definition, 0f, 0f);
	}
	
	public Circle(float radius, int definition, float initialX, float initialY){
		this.radius = radius;
		this.definition = definition;
		this.initialX = initialX;
		this.initialY = initialY;
		makefloatBuffer();
	}
	
	public float getRadius(){
		return this.radius;
	}
	
	public float getInitialX(){
		return this.initialX;
	}
	
	public float getInitialY(){
		return this.initialY;
	}
	
	public void draw(GL10 gl){
		gl.glVertexPointer(2, GL10.GL_FLOAT, 0, this.floatBuffer);
        gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, this.definition + 2);
	}
	
	private void makefloatBuffer(){
		float circleCoords[] = new float[(this.definition + 2) * 2];
        circleCoords[0] = this.initialX;
        circleCoords[1] = this.initialY;
        
        float deltaTheta = (float)(2* Math.PI / this.definition);
        float theta = 0f;
        for(int i = 1; i <= this.definition + 1; i++){
        	circleCoords[i*2] = ((float)Math.cos(theta)) * this.radius + this.initialX;
        	circleCoords[i*2 + 1] = ((float)Math.sin(theta)) * this.radius + this.initialY;
        	theta += deltaTheta;
    	}
        
        // initialize vertex Buffer for triangle  
        ByteBuffer circleByteBuffer = ByteBuffer.allocateDirect(
                // (# of coordinate values * 4 bytes per float)
        		circleCoords.length * 4); 
        circleByteBuffer.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
        this.floatBuffer = circleByteBuffer.asFloatBuffer();  // create a floating point buffer from the ByteBuffer
        this.floatBuffer.put(circleCoords);    // add the coordinates to the FloatBuffer
        this.floatBuffer.position(0);            // set the buffer to read the first coordinate
	}
}
